import express, { Request, Response } from "express"
import { UserMangeContext } from "../../../../context/UserMangeContext"
import { checkError } from "../../../../utils/token"
import { catchError } from "../../../../utils/common"
import { ChangeMemberMenuParams, MemberListParam, UserListParam, UserListReturnType } from "@Tea/model"
import { MenuContext } from "../../../../context/MenuContext"
import { MemberMangeContext } from "../../../../context/MemberMangeContext"
import { LoggedInRequest } from "../../../../types"

const memberList = express.Router()

memberList.post("/memberPageList", async (req: Request, res: Response) => {
  try {
    res.send({
      code: 1,
      msg: "查询成功",
      ...(await UserMangeContext.getInstance().getUserList(req.body as MemberListParam, "admin"))
    })
  } catch (e) {
    res.status(500).send({
      code: 0,
      msg: checkError(e)
    })
  }
})

memberList.get('/menuIds',(req: Request, res: Response) => {
  catchError(res, async () => {
    const query = req.query as any as UserListReturnType
    const result = await MenuContext.getInstance().getUserMenuIdById(query.userId)
    res.send({
      code: 1,
      msg: "查询成功",
      data: result[0].map((m)=>m.menuId)
    })
  })
})

memberList.put('/chagneMemberMenu', (req: LoggedInRequest, res: Response) => {
  catchError(res, async () => {
    const params = req.body as ChangeMemberMenuParams
    const result = await MemberMangeContext.getInstance().chagneMemberMenu(params, req.userInfo.userId)
    res.send({
      code: 1,
      msg: "菜单修改成功",
    })
  })
})

export default memberList